package com.apes.scm.masterdata.product.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.product.model.BrandType;
import com.apes.scm.masterdata.product.repository.BrandTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 功能：品牌分类 Service
 *
 * @author HuJianlin
 * @create 2018-03-15 14:35
 */
@Service("brandTypeService")
public class BrandTypeService extends DomainService {

    @Autowired
    BrandTypeRepository brandTypeRepository;

    public BrandType save(SimpleRequest request) {
        BrandType brandType = request.getO(BrandType.class);
        return brandTypeRepository.saveAndFlush(brandType);
    }

    public String delete(SimpleRequest request) {
        String id = request.get("id");
        Integer versions = request.get("version");
        BrandType brandType = brandTypeRepository.findOne(id);
        if (brandType == null) throw new RuntimeException("品牌分类【" + id + "】在系统中不存在。");
        if (brandType.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        brandTypeRepository.delete(brandType);
        return "删除成功";
    }

    /**
     * 功能：接收数据中心推送的消息
     *
     * @param request
     */
    public void receiveSave(SimpleRequest request) {
        Map map = request.getMap();
        BrandType brand = request.getO(BrandType.class);
        BrandType data = brandTypeRepository.findOne(brand.getId());
        if (data != null) brand.setVersion(data.getVersion());
        brandTypeRepository.saveAndFlush(brand);
    }
}
